package Map集合;

import java.sql.SQLType;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/*
* HashMap集合练习之键是String,值是Student
* */
public class HashMap集合练习1 {
    public static void main(String[] args) {
        /*创建HashMap集合对象*/
        HashMap<String, Student> hm = new HashMap<>();
        /*创建学生对象*/
        Student s1 = new Student("林青霞", 30);
        Student s2 = new Student("张曼玉", 35);
        Student s3 = new Student("王祖贤", 33);
        /*把学生添加到集合*/
        hm.put("itheima001",s1);
        hm.put("itheima002",s2);
        hm.put("itheima003",s3);
        /*方式1：键找值*/
        Set<String> keySet = hm.keySet();
        for (String key:keySet) {
            Student value = hm.get(key);
            System.out.println(key+","+value.getName()+","+value.getAge());
        }
        System.out.println("---------------");
        /*方式2：键值对对象找键和值*/
        Set<Map.Entry<String, Student>> entrySet = hm.entrySet();
        for (Map.Entry<String, Student> me:entrySet) {
            String key = me.getKey();
            Student value = me.getValue();
            System.out.println(key+","+value.getName()+","+value.getAge());
        }
    }
}
/*
* 学生类
* */
class Student{
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

}